$(document).ready(function(){
	loadZone();
	setYear();
	
	$("select[name=zone]#inputZone").change(function() {
		loadRoute();
	});
});

function loadZone() {
	$.ajax({
		url : "CS41002Srvl", 
		type : "POST",
		data : {
			process_type: "getZone"
		},
	//	anync:false,
		dataType:"JSON",
		success: function(data){
			var selected="";
			var options = '';
			options += '<option value="">-- เลือกเขต --</option>';
			for (var i = 0; i < data.values.length; i++) {
				options += '<option value="' + data.values[i] + '" '+selected+'>' + data.messages[i] +  '</option>';
			}
			$("select[name=zone]#inputZone").html(options);
		}
	});
}

function loadRoute(zoneCode) {
	var vaZoneCode = $( "#inputZone :selected").val();
	if(zoneCode!=""){
		vaZoneCode = zoneCode;
	}
	if(isNaN(vaZoneCode)==true){
		vaZoneCode = document.getElementById("inputZone").value;
	}
	$.ajax({
		url : "CS41002Srvl", 
		type : "POST",
		data : {
			process_type: "getRoute" , 
			zoneCode: vaZoneCode
		},
	//	anync:false,
		dataType:"JSON",
		success: function(data){
			var options = '';
			options += '<option value="">-- เลือกเส้นทาง --</option>';
			var selected ="";
			for (var i = 0; i < data.values.length; i++) {
				options += '<option value="' + data.values[i] + '" '+selected+'>' + data.messages[i] +  '</option>';
			}
			$("select#inputRoute").html(options);
		}
	});
}

function setYear() {
	var currentDate = new Date();
    var year = currentDate.getFullYear();
	year += 543;
	
	var selected="";
	var options = '';
	options += '<option value="">-- เลือกปี --</option>';
	for (var i = 0; i <= 3; i++) {
		options += '<option value="' + (year - i) + '" '+selected+'>' + (year - i) +  '</option>';
	}
	$("select[name=year]#inputYear").html(options);
}

function printReport() {

	var vaMonth = $("#inputMonth").val();
	var vnYear = $("#inputYear").val();
	var vnAccount = $("#inputAccount").val();
	var vnZone = $("#inputZone").val();
	var vnRoute = $("#inputRoute").val();
	var vnSiteId = $("#siteId").val();
	
	if(vaMonth.trim().length == 0 || vnYear.trim().length == 0 || vnAccount.trim().length == 0 
			|| vnZone.trim().length == 0 || vnRoute.trim().length == 0)
	{

		$("#modal_content").html("กรุณากรอกข้อมูลให้ครบถ้วน");
		$("#warningModal").modal();
		return;
	}else{

		var url = reporturl+"CS41002R.jsp?accountno="+vnAccount+"&month="+vaMonth+"&year="+vnYear+"&siteid="+vnSiteId+"&zoneid="+vnZone+"&routeid="+vnRoute;
		window.open(url);

	}
	
}